<template>
  <!-- !租客员工 -->
  <div class="room-member" style="height: 100%">
    <a-card size="small" style="height: 100%" v-if="hasOwner">
      <template v-if="currentOwner.id">
        <a-row type="flex" align="top" justify="space-between">
          <a-col>
            <a-dropdown
              v-if="ownerList.length > 1"
              overlayClassName="hide-scroller-bar"
              :overlayStyle="{
                height: '500px',
                overflow: 'hidden auto',
                borderRadius: '10px',
              }"
            >
              <div class="owner-name">
                <a-space>
                  <span>{{ currentOwner.owner_name }}</span>
                  <a-tag v-if="currentOwner.status == 0" color="#87d068">{{ currentOwner.owner_type }}</a-tag>
                  <a-tag v-else color="#777777">{{ currentOwner.owner_type }}</a-tag>
                  <DownOutlined />
                </a-space>
              </div>
              <template #overlay>
                <a-menu
                  @click="onOwnerChange"
                  style="border: 1px solid #f0f0f0"
                >
                  <a-menu-item v-for="item of ownerList" :key="item.id">
                    <span>{{ item.owner_name }}</span>
                    <a-tag style="margin-left: 10px;" v-if="item.status == 0" color="#87d068">{{ item.owner_type }}</a-tag>
                    <a-tag style="margin-left: 10px;" v-else color="#777777">{{ item.owner_type }}</a-tag>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
            <div class="owner-name" v-else>
              <span>{{ currentOwner.owner_name }}</span>
              <a-tag style="margin-left: 10px;" v-if="currentOwner.status == 0" color="#87d068">{{ currentOwner.owner_type }}</a-tag>
              <a-tag style="margin-left: 10px;" v-else color="#777777">{{ currentOwner.owner_type }}</a-tag>
            </div>
          </a-col>
          <a-col>
            <a-button type="link" @click="checkOwnerDetail">
              业户详情
            </a-button>
          </a-col>
        </a-row>
        <a-table
          :dataSource="memberList"
          :columns="MEMBER_COLUMNS"
          :pagination="pagination"
          @change="handleTableChange"
          rowKey="id"
          :loading="loading"
          size="small"
          bordered
          class="mt-10"
        >
          <template #title>
            <a-row type="flex" align="middle" justify="space-between">
              <a-col>业户人员列表</a-col>
              <a-col>
                <a-button type="primary" @click="onMemberEdit()">
                  <template #icon>
                    <PlusOutlined />
                  </template>
                  业户人员
                </a-button>
              </a-col>
            </a-row>
          </template>
          <template #bodyCell="{ text, record, index, column }">
            <template v-if="column.key === 'status'">
              <a-badge
                v-if="record.status == 0"
                status="warning"
                :text="record.status_txt"
              />
              <a-badge
                v-if="record.status == 1"
                status="success"
                :text="record.status_txt"
              />
              <a-badge
                v-if="record.status == 4"
                status="error"
                :text="record.status_txt"
              />
            </template>
            <template v-if="column.key === 'action'">
              <a-button type="link" @click="onMemberEdit(record)">
                编辑
              </a-button>
              <a-button type="link" danger @click="onMemberRemove(record)">
                删除
              </a-button>
            </template>
          </template>
        </a-table>
      </template>
    </a-card>
    <div class="no-owner" v-else>
      <a-empty description="当前房源未绑定业户" />
    </div>

    <!-- 租客详情 -->
    <OwnerDetailDrawer
      :visible="ownerDetailVisible"
      :id="currentOwner.id"
      @cancel="onOwnerDetailClose"
    />

    <!-- 添加编辑租客员工 -->
    <HouseholdEdit
      :visible="editVisible"
      :id="editId"
      :ownerInfo="currentOwner"
      @onCancel="editVisible = false"
      @success="onMemberEditSuccess"
    />
  </div>
</template>
<script>
import { defineComponent, reactive, toRefs, computed } from "vue";
import { PlusOutlined, DownOutlined } from "@ant-design/icons-vue";
import OwnerDetailDrawer from "../../../views/village/OwnerManage/modules/OwnerDetailDrawer.vue";
import HouseholdEdit from "../../../views/village/HouseholdManagement/modules/HouseholdEdit.vue";
import request from "../../../common/utils/request";
import { useRouter } from "vue-router";
import BLCOMMON from "bl-common-vue3";
import utils from "../../../common/utils/utils";
import { message } from "ant-design-vue";

const LANG = {
  member: BLCOMMON.utils.L("租客员工"),
  owner: BLCOMMON.utils.L("租客"),
};

export default defineComponent({
  name: "RoomMember",
  props: {
    detail: {
      type: Object,
      default: () => ({}),
    },
  },
  components: {
    PlusOutlined,
    DownOutlined,
    OwnerDetailDrawer,
    HouseholdEdit,
  },
  setup(props, { emit }) {
    const ROUTER = useRouter();

    const state = reactive({
      LANG,

      ownerList: [],
      currentOwner: {},
      hasOwner: true,
      ownerDetailVisible: false,

      memberList: [],
      loading: false,
      editVisible: false,
      editId: 0,

      page: 1,
      pageSize: 20,
      total: 0,
    });

    /** 添加编辑删除成员 */
    const onMemberEdit = (current) => {
      state.editId = current ? current.id : 0;
      state.editVisible = true;
    };
    const onMemberEditSuccess = () => {
      state.editVisible = false;
      getMemberList();
    };

    const onMemberRemove = (current) => {
      utils.modalConfirm({
        content: `确认删除该业户人员吗？`,
        okType: "danger",
        centered: true,
        onOk: () => {
          request
            .del("/village", "/org/user/delete", {
              id: current.id,
              village_id: props.detail.village_id,
            })
            .then(() => {
              message.success("删除成功");
              getMemberList();
            });
        },
      });
    };

    /** 查看租客详情 */
    const checkOwnerDetail = () => {
      state.ownerDetailVisible = true;
    };
    const onOwnerDetailClose = () => {
      state.ownerDetailVisible = false;
      getRoomOwnerList();
    };

    /** 选中租客 */
    const onOwnerChange = (e) => {
      const { key } = e;
      state.currentOwner = state.ownerList.find((it) => it.id == key);
      getMemberList();
    };

    /** 获取租客列表 */
    const getRoomOwnerList = () => {
      request
        .get("/village", "/project/room/getOwnerList", {
          build_id: props.detail.build_id,
          room_id: props.detail.id,
        })
        .then((res) => {
          state.ownerList = [];
          if (res.list && res.list.length > 0) {
            state.hasOwner = true;
            state.ownerList = res.list;
            onOwnerChange({ key: res.list[0].id });
          } else {
            state.hasOwner = false;
          }
        });
    };

    // 分页
    const pagination = computed(() => ({
      total: state.total,
      current: state.page,
      pageSize: state.pageSize,
      showSizeChanger: true,
    }));
    const handleTableChange = (pager, filters, sorter) => {
      state.page = pager.current;
      state.pageSize = pager.pageSize;
      getMemberList();
    };

    const getMemberList = () => {
      state.loading = true;
      request
        .get("/village", "/rentControl/room/tenantUser", {
          owner_id: state.currentOwner.id,
          page: state.page,
          pageSize: state.pageSize,
        })
        .then((res) => {
          state.memberList = res.list;
          state.total = res.total;
        })
        .finally(() => {
          state.loading = false;
        });
    };

    const init = () => {
      getRoomOwnerList();
    };

    init();

    return {
      MEMBER_COLUMNS,
      BLCOMMON,
      ...toRefs(state),
      onOwnerChange,
      checkOwnerDetail,
      onOwnerDetailClose,
      onMemberEdit,
      onMemberEditSuccess,
      onMemberRemove,
      pagination,
      handleTableChange,
    };
  },
});

const MEMBER_COLUMNS = [
  {
    title: "姓名",
    dataIndex: "name",
  },
  {
    title: "身份",
    dataIndex: "type_txt",
  },
  {
    title: "电话号码",
    dataIndex: "phone",
  },
  {
    title: "状态",
    dataIndex: "status_txt",
    key: "status",
  },
  {
    title: "操作",
    dataIndex: "id",
    key: "action",
  },
];
</script>

<style lang="less" scoped>
.no-owner {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.owner-name {
  font-size: 15px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
}
</style>
